package wx;

import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import util.XmlUtil;
import wx.pojo.PayConfig;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class WXCallbackController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WXCallbackController.class);

    @ApiOperation("微信支付-回调")
    @PostMapping("/v1/wx/pay/callback")
    public void wxNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String requestStr = br.lines().collect(Collectors.joining());
        br.close();
        String resXml = "";
        LOGGER.info("微信支付-回调，接收到的报文:{}", requestStr);
        Map<String, String> map = XmlUtil.parseXml(requestStr);
        String returnCode = map.get("return_code");
        if ("SUCCESS".equals(returnCode)) {
            if (WXPayUtil.checkSign(map, "wxMchKey")) {
                String outTradeNo = map.get("out_trade_no");
                String transactionId = map.get("transaction_id");
                String attach = map.get("attach");
                resXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
            } else {
                resXml = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[SIGN_ERROR]]></return_msg></xml>";
            }
        } else {
            resXml = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";
        }
        LOGGER.info("微信支付-回调结束,resXml：{}", resXml);
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        out.write(resXml.getBytes());
        out.flush();
        out.close();
    }

    @ApiOperation("微信退款-回调")
    @PostMapping("/v1/wx/refund/callback")
    public void wxRefundNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String requestStr = br.lines().collect(Collectors.joining());
        br.close();
        String resXml = "";
        LOGGER.info("微信退款-回调,接收到的报文:{}", requestStr);
        Map<String, String> map = XmlUtil.parseXml(requestStr);
        String returnCode = map.get("return_code");
        if ("SUCCESS".equals(returnCode)) {
            String reqInfo = map.get("req_info");
            String mchId = map.get("mch_id");
            String appId = map.get("appid");
            Map<String, String> decodeMap = WXPayUtil.decodeReqInfo(new PayConfig(),reqInfo);

            resXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        } else {
            resXml = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";
        }
        LOGGER.info("微信退款-回调结束,resXml：{}", resXml);
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        out.write(resXml.getBytes());
        out.flush();
        out.close();
    }

}
